package com.buercorp.wangyu.core2.ch1.streams.collecting;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;

/**
 * 收集结果
 * @version 1.02 2019-08-28
 * @author Cay Horstmann
 */
public class CollectingResults
{
   public static Stream<String> noVowels() throws IOException
   {
      var contents = Files.readString(
         Path.of("../gutenberg/alice30.txt"));
      // 使用正则表达式对读取的内容进行分割
      List<String> wordList = List.of(contents.split("\\PL+"));
      Stream<String> words = wordList.stream();
      // replaceAll 将列表中的每个字符串中的所有元音字母（a, e, i, o, u，不区分大小写）替换为空字符串（即删除这些元音字母）
      return words.map(s -> s.replaceAll("[aeiouAEIOU]", ""));
   }

   public static <T> void show(String label, Set<T> set)
   {
      System.out.print(label + ": " + set.getClass().getName());
      System.out.println("["
         + set.stream().limit(10).map(Object::toString).collect(Collectors.joining(", ")) 
         + "]");
   }

   public static void main(String[] args) throws IOException
   {
      // limit 获取前10个
      Iterator<Integer> iter = Stream.iterate(0, n -> n + 1).limit(10).iterator();
      while (iter.hasNext())
         System.out.println(iter.next());

      Object[] numbers = Stream.iterate(0, n -> n + 1).limit(10).toArray();
      System.out.println("Object array:" + numbers); 
         // Note it's an Object[] array


      // 尝试将Object类型的数组转化为Integer类型的数组
      try
      {
         var number = (Integer) numbers[0]; // OK
         System.out.println("number: " + number);
         System.out.println("The following statement throws an exception:");
         var numbers2 = (Integer[]) numbers; // Throws exception
      }
      catch (ClassCastException e)
      {
         System.out.println(e);
      }

      // Stream.iterate生成从零开始的整数流
      Integer[] numbers3 = Stream.iterate(0, n -> n + 1)
         .limit(10)
         .toArray(Integer[]::new);
      System.out.println("Integer array: " + numbers3); 
         // Note it's an Integer[] array

      Set<String> noVowelSet = noVowels().collect(Collectors.toSet());
      show("noVowelSet", noVowelSet);

      TreeSet<String> noVowelTreeSet = noVowels().collect(
         Collectors.toCollection(TreeSet::new));
      show("noVowelTreeSet", noVowelTreeSet);
      // joining将流中的元素连接成一个字符串
      String result = noVowels().limit(10).collect(Collectors.joining());
      System.out.println("Joining: " + result);
      result = noVowels().limit(10)
              // 用逗号连接起来
         .collect(Collectors.joining(", "));
      System.out.println("Joining with commas: " + result);

      // Collectors.summarizingInt() 方法来生成一个整数类型的统计信息
      // IntSummaryStatistics 是一个类，用于存储整数类型的统计信息，如总和、平均值、最大值、最小值等
      IntSummaryStatistics summary = noVowels().collect(
         Collectors.summarizingInt(String::length));
      double averageWordLength = summary.getAverage();
      double maxWordLength = summary.getMax();
      System.out.println("Average word length: " + averageWordLength);
      System.out.println("Max word length: " + maxWordLength);
      System.out.println("forEach:");
      noVowels().limit(10).forEach(System.out::println);
   }
}
